package jp.tkym.labs.store.sql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import jp.tkym.labs.store.ormap.OrMapObjectProvider;

class PreparedStatementFactory{
	private Connection connection;
	private SqlStatementGenerator generator = new SqlStatementGenerator();
	private boolean log = false;
	PreparedStatementFactory(Connection connection){
		this.connection = connection;
	}
	
	PreparedStatement nativeStatement(String sql) throws SQLException{
		if(log)System.out.println("create PreparedStatement:"+sql);
		return connection.prepareStatement(sql);
	}
	
	<T> PreparedStatement key(Class<T> cls, String laterSql) throws SQLException{
		String sql = generator.key(OrMapObjectProvider.getInstance().get(cls), laterSql);
		return nativeStatement(sql);
	}
	
	<T> PreparedStatement entity(Class<T> cls, String laterSql) throws SQLException{
		String sql = generator.entity(OrMapObjectProvider.getInstance().get(cls), laterSql);
		return nativeStatement(sql);
	}
	
	<T> PreparedStatement drop(Class<T> cls) throws SQLException{
		String sql = generator.drop(OrMapObjectProvider.getInstance().get(cls));
		return nativeStatement(sql);
	}
	<T> PreparedStatement create(Class<T> cls) throws SQLException{
		String sql = generator.create(OrMapObjectProvider.getInstance().get(cls));
		return nativeStatement(sql);
	}
	<T> PreparedStatement update(Class<T> cls) throws SQLException{
		String sql = generator.update(OrMapObjectProvider.getInstance().get(cls));
		return nativeStatement(sql);
	}
	<T> PreparedStatement insert(Class<T> cls) throws SQLException{
		String sql = generator.insert(OrMapObjectProvider.getInstance().get(cls));
		return nativeStatement(sql);
	}
	<T> PreparedStatement delete(Class<T> cls) throws SQLException{
		String sql = generator.delete(OrMapObjectProvider.getInstance().get(cls));
		return nativeStatement(sql);
	}
}